/*
 * @Author jiangzhiwei
 * @Date 2023-07-23 18:36:02
 * @LastEditors jiangzhiwei
 * @LastEditTime 2023-07-28 11:32:39
 * @FilePath \test-cron\src\components\yc-cron\components\options\common-week-day-option\index.js
 * @Description 第几个星期几选项
 */
import { defineComponent } from 'vue-demi'
import { emitValue } from '../../../utils/componentutil'
import { getWeekMap } from '../../../utils/config'
import { QUARTS_TYPE, VALUE_NAME, VALUE_EMIT_NAME, PARSE_ERROR_EMIT_NAME } from '../../../utils/constant'
import { h } from '../../../utils/domutil'
import { setValue } from '../../../utils/proputil'
import optionMixin from '../mixins/optionmixin'
import CommonLi from '../../base/common-li'
import CommonInputNumber from '../../base/common-input-number'

const NAME = 'yc-cron-week-day-option'
const regexp = /^(\d)#(\d)$/

export default defineComponent({
  name: NAME,
  mixins: [optionMixin],
  data () {
    return {
      /** 第几个周几。这里与显示值是反着的 */
      time: [1, 1],
      /** 最大值。0~7 或 1~7最大值都是7 */
      max: 7,
      /** 时间起始值。linux与spring是从0开始，quarts是从1开始 */
      startNum: this.type === QUARTS_TYPE ? 1 : 0,
      valueMap: getWeekMap(this.type)
    }
  },
  watch: {
    /** 根据表达式变化来设置选项内容 */
    expression: {
      handler (value) {
        if (value && value.indexOf('#') !== -1) { // 表示匹配
          const arr =  regexp.exec(value)
          if (arr) { // arr存在表示匹配，即格式正确
            /** 周几 */
            const num1 = Number.parseInt(arr[1], 10)
            /** 第几个 */
            const num2 = Number.parseInt(arr[2], 10)
            if (num1 < this.startNum) {
              emitValue(this, `第一个数字不能小于${this.startNum}`, PARSE_ERROR_EMIT_NAME)
            } else if (num1 > this.max) {
              emitValue(this, `第一个数字不能大于${this.max}`, PARSE_ERROR_EMIT_NAME)
            } else if (num2 < 1) {
              emitValue(this, '第二个数字不能小于1', PARSE_ERROR_EMIT_NAME)
            } else if (num2 > 5) { // 一个月最多5周
              emitValue(this, '第二个数字不能大于5', PARSE_ERROR_EMIT_NAME)
            } else {
              this.time = [num1, num2]
              emitValue(this, this.label)
            }
          } else {
            emitValue(this, '格式错误', PARSE_ERROR_EMIT_NAME)
          }
        } else { // 不匹配
          this.tryCancelCheck()
        }
      },
      immediate: true
    }
  },
  methods: {
    /** 更新 */
    onUpdate (value, index) {
      setValue(this.time, index, value)
      // emitValue(this, this.label)
      this.tryEmitExpression()
    },
    /** 获取表达式 */
    getExpression () {
      return this.time.join('#')
    }
  },
  render () {
    return h(
      CommonLi,
      {
        props: { [VALUE_NAME]: this[VALUE_NAME], label: this.label },
        on: { [VALUE_EMIT_NAME]: this.onInput }
      },
      [
        this.descriptions[0],
        h(
          CommonInputNumber,
          {
            props: {
              [VALUE_NAME]: this.time[1],
              min: 1,
              max: 5,
              controlsPosition: 'right',
              size: 'small'
            },
            on: { [VALUE_EMIT_NAME]: value => this.onUpdate(value, 1) }
          }
        ),
        this.descriptions[1],
        h(
          CommonInputNumber,
          {
            props: {
              [VALUE_NAME]: this.time[0],
              min: this.startNum,
              max: 7,
              valueMap: this.valueMap,
              controlsPosition: 'right',
              size: 'small'
            },
            on: { [VALUE_EMIT_NAME]: value => this.onUpdate(value, 0) }
          }
        ),
        this.descriptions[2]
      ]
    )
  }
})
